package com.wyl.mobileplayer.model;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.wyl.mobileplayer.model.bean.Media;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 时间：2018/6/24 0024 21:31
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author WangYoule
 * @qq 270628297
 */
public class VideoModel implements IMediaModel {
    private Context context;

    public VideoModel(Context context) {
        this.context = context;
    }

    /**
     * 从本地的sdcard得到数据
     * 1. 从内容提供者里面获取视频
     * 2. 从sd卡中遍历 (理论)
     */
    public void getLocalVideo(final IMediaModel.CallBack callBack) {
        Observable.create(new ObservableOnSubscribe<List<Media>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Media>> emitter) throws Exception {
                List<Media> mediaList = new ArrayList<>();
                ContentResolver resolver = context.getContentResolver();
                Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                String[] projection = new String[]{
                        MediaStore.Video.Media.DISPLAY_NAME,    //视频在sdcard中的名称
                        MediaStore.Video.Media.DATA,            //视频在sdcard中的路径
                        MediaStore.Video.Media.DURATION,        //视频总时长
                        MediaStore.Video.Media.SIZE,            //视频总大小
                };
                Cursor cursor = resolver.query(uri, projection, null, null, null);
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        Media media = new Media();
                        String display_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                        Long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                        Long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
                        media.display_name = display_name;
                        media.data = data;
                        media.duration = duration;
                        media.size = size;
                        mediaList.add(media);
                    }
                    cursor.close();
                }
                emitter.onNext(mediaList);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Media>>() {
                    @Override
                    public void accept(List<Media> media) throws Exception {
                        callBack.onSucceed(media);
                    }
                });


    }
}
